package com.share.web.action.oa.org;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.share.bean.oa.org.Organization;
import com.share.commons.base.query.PageView;
import com.share.commons.util.DateUtil;
import com.share.commons.util.EncodingUtil;
import com.share.service.oa.org.OrganizationManager;
import com.share.web.action.base.BaseActionSupport;
import com.share.web.formbean.oa.hr.OrganizationForm;

@Controller()
@Scope("prototype")
public class OrganizationAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;

	private Organization org;
	
	private OrganizationForm orgform ;

	@Resource
	private OrganizationManager orgMgr;
	
	@Resource
	private PageView<Organization> pageView ;
	
	public String showScrollOrg() {
		try {
			if(null != orgform) {
				StringBuffer urlParam = new StringBuffer() ;
				
				if(null != orgform.getStartDate() && null != orgform.getEndDate()) {
					urlParam.append("&orgform.startDate=" + DateUtil.format(orgform.getStartDate(), DateUtil.TYPE_DATE))
					.append("&orgform.endDate=" + DateUtil.format(orgform.getEndDate(), DateUtil.TYPE_DATE));
				} else if(null != orgform.getKeyword() && !orgform.getKeyword().equals("")) {
					orgform.setKeyword(EncodingUtil.encodeURI(orgform.getKeyword())) ;
					urlParam.append("&orgform.keyword=" + orgform.getKeyword()) ;
				}
				
				//将参数保存到request范围，便于分页
				this.getBaseform().getRequest().setAttribute("urlParam",urlParam.toString()) ;
				this.getBaseform().getRequest().setAttribute("orgform",orgform) ;
			}
			pageView.setPageModel(this.orgMgr.loadScrollDataOrgManager(orgform, pageView.getFirstResult(),pageView.getMaxResult())) ;
			
			this.getBaseform().getRequest().setAttribute("pageView",pageView) ;
			
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		return "showScrollOrg" ;
	}

	public String addOrg() {
		try {
			this.orgMgr.saveOrgManager(org);

			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			this.saveMessage("添加机构错误，请与管理员联系") ;
			this.log.info("添加机构失败，错误原因：" + e.getMessage()) ;
			return "errors";
		}
	}
	
	public String deleteOrg() {
		try {
			this.orgMgr.deleteOrgManager(this.org.getOrgCode()) ;
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			this.saveMessage("删除机构错误，该机构还有部门未删除") ;
			this.log.info("删除机构失败，错误原因：" + e.getMessage()) ;
			return "messages";
		}
	}

	public String updateOrg() {
		try {
			this.orgMgr.updateOrgManager(org);

			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace() ;
			this.saveMessage("修改错误，请与管理员联系") ;
			this.log.info("修改机构失败，错误原因：" + e.getMessage()) ;
			return "errors";
		}
	}

	public String loadByIdOrg() {
		try {
			this.getBaseform().getRequest().setAttribute("org",this.orgMgr.loadByIdOrgManager(this.org.getOrgCode()));
			
			if ("updateOrg".equals(this.getBaseform().getType())) {
				return "updateOrg";
			} else {
				return "queryOrg";
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.saveMessage("按机构代码查询错误，请与管理员联系") ;
			this.log.info("按机构代码查询失败，错误原因：" + e.getMessage()) ;
			return "errors";
		}
	}


	public Organization getOrg() {
		return org;
	}

	public void setOrg(Organization org) {
		this.org = org;
	}
	
	public PageView<Organization> getPageView() {
		return pageView;
	}

	public void setPageView(PageView<Organization> pageView) {
		this.pageView = pageView;
	}
	public OrganizationForm getOrgform() {
		return orgform;
	}

	public void setOrgform(OrganizationForm orgform) {
		this.orgform = orgform;
	}

}
